<?php
namespace addon\payment\controller;
use app\common\controller\AddonFrontBase;
use tpfcore\Core;
use think\Cookie;
use tpfcore\payment\Alipay;
// +----------------------------------------------------------------------
// | Author: yaoyihong <510974211@qq.com>
// +----------------------------------------------------------------------
/**
 * ============================================================================
 * 版权所有 2017-2077 tpframe工作室，并保留所有权利。
 * @link http://www.tpframe.com/
 * @copyright Copyright (c) 2017 TPFrame Software LLC
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！未经本公司授权您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布
 * ============================================================================
 */
class Order extends AddonFrontBase
{
	// 测试支付
	public function test(){
		// 支付参数在发起位置（调起支付的地方）通过传递订单ID或订单号过来去数据库取
		$pay_param=[
            'body'=>"支付充值测试",        // 商品描述
            'out_trade_no'=>date('Ymdhis').rand(10000,99999),       // 商户订单号 
            'total_amount'=>"1",            //总金额
            'subject'=>"付款",                  // 商品的标题/交易标题/订单标题/订单关键字等
        ];
        (new Alipay(Core::loadAddonModel("Payment")->getPaymentConfig("alipay",'pc')))->pay($pay_param,"pc");
	}

	/*
        支付2(一种更灵活的支付方式，handle：支付发起，code：支付代码)
        http://xxx.com/payment/order/pay/handle/weixin/code/mp
        http://xxx.com/payment/order/pay/handle/weixin/code/app
        http://xxx.com/payment/order/pay/handle/weixin/code/pc

        http://xxx.com/payment/order/pay/handle/alipay/code/app
        http://xxx.com/payment/order/pay/handle/alipay/code/pc
        http://xxx.com/payment/order/pay/handle/alipay/code/wap
    */
    public function pay()
    {

        if(!isset($this->param['order_sn'])){

            $this->jump([-1,"请传入订单号"]);

        }

        $order = Core::loadAddonModel("Order","mall")->getOrder(['where'=>['order_sn'=>$this->param['order_sn']]]); 

        if(empty($order)){

             $this->jump([-1,"没有要支付的订单"]);

        }

        $order_info = $order[0]->toArray();

        $handle=$order_info['pay_handle'];

        $code=$order_info['pay_code'];

        $pay_param=[
            'body'=>empty($order_info['order_info'])?"商品描述":$order_info['order_info'],        // 商品描述
            'out_trade_no'=>$this->param['order_sn'],       // 商户订单号 
            'total_amount'=>$order_info['goods_price'],            //总金额
            'subject'=>"付款",                  // 商品的标题/交易标题/订单标题/订单关键字等
        ];
        $handle_class="\\tpfcore\payment\\".ucfirst($handle);

        $result=(new $handle_class(Core::loadAddonModel("Payment")->getPaymentConfig($handle,$code)))->pay($pay_param,$code);
        if($handle=="weixin" && $code=="mp"){
            return $this->addonTemplate('wx_jspay',[
                'jsApiParameters'=>$result['jsApiParameters'],
                'editAddress'=>$result['editAddress'],
                'succeed'=>'success.html',      //成功跳转页面
                'fail'=>'error.html'            //失败跳转页面
            ]);
        }else{
            die($result);
        }
    }

    // 生成订单接口
    public function create(){
    	IS_POST && $this->jump(Core::loadAddonModel("Order","mall")->createOrder($this->param));
    }
}